package com.chookss.mine.personal.upload;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.StaticClass;
import com.chookss.home.entity.UploadEntity;
import com.chookss.tools.Utils;
import com.chookss.view.BottomListsDialog;
import com.chookss.view.SelectDialogListener;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.lvgroup.hospital.base.BaseAct;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PublishDocActivity extends BaseAct {
    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.ivIconType)
    ImageView ivIconType;
    @BindView(R.id.edKey)
    EditText edKey;
    @BindView(R.id.tvOriginal)
    TextView tvOriginal;
    @BindView(R.id.ivBg)
    View ivBg;
    @BindView(R.id.ivChoice)
    ImageView ivChoice;
    @BindView(R.id.ivChoice2)
    ImageView ivChoice2;
    @BindView(R.id.rlChoice)
    RelativeLayout rlChoice;
    @BindView(R.id.tv_publish)
    TextView tvPublish;
    private BottomListsDialog originalDialog;
    private String original = "1";//(0-否 1-是)',
    private int shareFlag = 1; //1分享 0不允许分享
    private UploadEntity uploadEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_doc);
        ButterKnife.bind(this);
        initState();
    }

    @Override
    public void initData() {
        commonTitleTxt.setText("发布");
        uploadEntity= (UploadEntity) getIntent().getSerializableExtra("data");
        List<String> originalList = new ArrayList<>();
        originalList.add("原创");
        originalList.add("非原创");
        originalList.add("取消");
        originalDialog = new BottomListsDialog(this, originalList, new SelectDialogListener() {
            @Override
            public void onCancel() {
            }

            @Override
            public void onListItemClick(int position, List<String> list) {
                if (tvOriginal == null) {
                    return;
                }
                if (position == 0) {
                    original = "1";
                    tvOriginal.setText("原创");
                    originalDialog.setSelect(0);
                } else if (position == 1) {
                    original = "0";
                    tvOriginal.setText("非原创");
                    originalDialog.setSelect(1);
                }
                originalDialog.dismiss();
            }
        });
        originalDialog.setSelect(0);
        String fileType = uploadEntity.getDataBase().getFileType();//文件类型1--word 2 --excel 3--PPT  4pdf
        if ("1".equals(fileType)) {
            Glide.with(this).load(R.drawable.icon_w_80).into(ivIconType);
        } else if ("2".equals(fileType)) {
            Glide.with(this).load(R.drawable.icon_x_80).into(ivIconType);
        } else if ("3".equals(fileType)) {
            Glide.with(this).load(R.drawable.icon_p_80).into(ivIconType);
        } else if ("4".equals(fileType)) {
            Glide.with(this).load(R.drawable.icon_pdf_80).into(ivIconType);
        } else {
            Glide.with(this).load(R.drawable.icon_mr_80).into(ivIconType);
        }
        tvTitle.setText(uploadEntity.getDataBase().getDataTitle());
    }

    @OnClick({R.id.common_title_back,  R.id.tvOriginal, R.id.rlChoice, R.id.tv_publish})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.tvOriginal:
                if (originalDialog != null) {
                    originalDialog.show();
                }
                break;
            case R.id.rlChoice:
                if (ivBg == null) {
                    return;
                }
                if (shareFlag == 1) {
                    shareFlag = 0;
                    ivBg.setVisibility(View.GONE);
                    ivChoice.setVisibility(View.GONE);
                    ivChoice2.setVisibility(View.VISIBLE);
                } else {
                    shareFlag = 1;
                    ivBg.setVisibility(View.VISIBLE);
                    ivChoice.setVisibility(View.VISIBLE);
                    ivChoice2.setVisibility(View.GONE);
                }
                break;
            case R.id.tv_publish:
                if (Urls.antiShake.check()) {
                    return;
                }
                uploadEntity.getDataBase().setOriginal(original);
                uploadEntity.getDataBase().setShareFlag(shareFlag+"");
                uploadEntity.setUploadStatus(2);
                uploadEntity.setUploadTag(Utils.getCurrentTime2());
                uploadEntity.setProvideEmployeeCode(new ShareUtils().getString(this, StaticClass.EMPLOYEECODE, ""));
                intent = new Intent(this, UploadFileService.class);
                Bundle bundle = new Bundle();
                //须定义一个list用于在budnle中传递需要传递的ArrayList<Object>,这个是必须要的
                ArrayList bundlelist = new ArrayList();
                ArrayList< UploadEntity> docList = new ArrayList<>();
                docList.add(uploadEntity);
                bundlelist.add(docList);
                bundle.putParcelableArrayList("data", bundlelist);
                intent.putExtras(bundle);
                intent.putExtra("resourceType","1");
                startService(intent);
                finish();
                break;
        }
    }

}
